<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Fangjian;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Dept;

class FangjianController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Fangjian(), function (Grid $grid) {
            // $grid->column('id')->sortable();
            $grid->model()->with(['dept']);
            $grid->column('bianhao');
            $grid->column('fangjianname');

            $grid->column('louhao');
            $grid->column('louceng');


            // $grid->column('dept_id');
            $grid->column('dept.deptname', '所属科室')->link(function ($value) {
                $deptid = Dept::where('deptname', $value)->first()->id;
                return admin_url('depts/' . $deptid);
            });

            $grid->column('weizhi');
            $grid->column('zrrname');
            $grid->column('fangjianpics');
            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Fangjian(), function (Show $show) {
            $show->field('id');
            $show->field('bianhao');
            $show->field('fangjianname');
            $show->field('weizhi');
            $show->field('dept_id');
            $show->field('fangjianpics');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Fangjian(), function (Form $form) {
            $form->display('id');
            $form->text('bianhao');
            $form->text('fangjianname');
            $form->text('weizhi');
            $form->text('dept_id');
            $form->text('fangjianpics');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
